package day_2022_9_to_12.Day;

import java.util.Stack;

/**
 * @author haomin
 * @date 2022/09/14 23:47
 **/
public class Day12_5_Num394_DecodeString {
    class Solution {
        public String decodeString(String s) {
            Stack<Integer> mulStack = new Stack<>();
            Stack<StringBuffer> sbStack = new Stack<>();
            StringBuffer sb = new StringBuffer();
            int multiply = 0;
            for(char c : s.toCharArray()){
                if(Character.isDigit(c)) {
                    multiply = multiply*10 + c - '0';
                } else if(Character.isLetter(c)) {
                    sb.append(c);
                } else if(c == '['){
                    mulStack.add(multiply);
                    sbStack.add(sb);
                    multiply = 0;
                    sb = new StringBuffer();
                }else if(c == ']'){
                    int mulTemp = mulStack.pop();
                    StringBuffer sbTemp = sbStack.pop();
                    while(mulTemp-- > 0) sbTemp.append(sb);
                    sb = sbTemp;
                }
            }
            return sb.toString();
        }
    }
}